package com.example.study.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.study.common.api.ResultUtil;
import com.example.study.mapper.StudentMapper;
import com.example.study.model.StudentVO;
import com.example.study.model.StudentPO;
import com.example.study.service.RedisService;
import com.example.study.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Calendar;

@Service
@Slf4j
public class StudentServiceImpl extends ServiceImpl<StudentMapper, StudentPO> implements StudentService {

    private final String KEY_YEAR_CODE = "redis:key:year";
    private final String KEY_STUDENT_CODE = "redis:key:studentCode";

    @Resource
    private StudentMapper studentMapper;

    @Resource
    private RedisService redisService;

    @Override
    public ResultUtil saveStudent(StudentVO studentVO) {
        int year = Calendar.getInstance().get(Calendar.YEAR);
        if (redisService.hasKey(KEY_YEAR_CODE)){
            int redisYear = (int) redisService.get(KEY_YEAR_CODE);
            if (redisYear != year){
                redisService.set(KEY_YEAR_CODE, year);
                redisService.set(KEY_STUDENT_CODE, 1);
            }else{
                redisService.incr(KEY_STUDENT_CODE, 1);
            }
        }else{
            redisService.set(KEY_YEAR_CODE, year);
            redisService.set(KEY_STUDENT_CODE, 1);
        }

        int code = (int) redisService.get(KEY_STUDENT_CODE);
        String studentCode = year + String.format("%06d", code);

        StudentPO studentPO = new StudentPO();
        BeanUtils.copyProperties(studentVO, studentPO);
        studentPO.setStudentCode(studentCode);

        int row = studentMapper.insert(studentPO);
        if (row > 0){
            return ResultUtil.success(studentPO);
        }
        return ResultUtil.failed("插入学生失败");
    }

    @Override
    public ResultUtil getStudentPage(int pageIndex, int pageSize) {
        Page<StudentPO> page = new Page<>(pageIndex, pageSize);
        QueryWrapper<StudentPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("student_code");
        IPage<StudentPO> iPage = studentMapper.selectPage(page, queryWrapper);
        return ResultUtil.success(iPage);
    }

    @Override
    public ResultUtil getStudentCriteriaPage(int pageIndex, int pageSize, StudentVO studentVO) {
        //把StudentVO转换成StudentPO
        StudentPO studentPO = new StudentPO();
        BeanUtils.copyProperties(studentVO, studentPO);

        Page<StudentPO> page = new Page<>(pageIndex, pageSize);
        QueryWrapper<StudentPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(studentPO);
        queryWrapper.orderByAsc("student_code");
        IPage<StudentPO> iPage = studentMapper.selectPage(page, queryWrapper);
        return ResultUtil.success(iPage);
    }

    @Override
    public ResultUtil updateStudent(StudentVO studentVO) {
        //把StudentBO转换成StudentPO
        StudentPO studentPO = new StudentPO();
        BeanUtils.copyProperties(studentVO, studentPO);

        int row = studentMapper.updateById(studentPO);
        if (row > 0){
            return ResultUtil.success(true);
        }
        return ResultUtil.failed("修改学生信息失败");
    }

    @Override
    public ResultUtil deleteStudent(Long id) {
        StudentPO studentPO = new StudentPO();
        studentPO.setId(id);
        studentPO.setStatus(1);
        int row = studentMapper.updateById(studentPO);
        if (row > 0){
            return ResultUtil.success(true);
        }
        return ResultUtil.failed("删除学生信息失败");
    }

}